# -*-coding:utf-8 -*-
import socket, traceback, os, sys, time, struct
import qcfg
import HTCUtils

def cmdSend(aCmd):
    try:
        s = socket.socket()
        s.connect((qcfg.TMP_IP,qcfg.TMP_PORT))
        s.sendall(aCmd)
        recv = s.recv(1024)
        s.close()
        return recv
    except socket.gaierror, e:
        print e
    except socket.error, e:
        print e
        
    return ''

def cmdSplit(aName,aStr):
    if aStr == "<{true}[||]>" or aStr == "<{false}>":
        return -1
    #HTCUtils.rctLogger.info(aName + "=" + aStr)
    tmpRet =aStr.split("|")[1]
    return tmpRet

#打印信息
def dbSelectGetOrderPrint(aSaleno) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_GET_ORDERPRINT('" + aSaleno +"')</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectGetOrderPrint",tmpRet)
    return tmpRet


#获取账单列表
def dbSelectGetOrderList(aCard,aIndx) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_GET_ORDERLIST(" + aCard + "," + aIndx + ")</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectGetOrderList",tmpRet)
    return tmpRet



#账单详情
def dbSelectGetOrderMsg(aSaleno) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_GET_ORDERMSG('" + aSaleno + "')</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectGetOrderMsg",tmpRet)
    return tmpRet




#更新交易历史信息 :设备id
# 成功                账户余额
# 传入参数为空         返回 -1
#不存在此设备编号     返回 -2
#不存在此卡           返回 -3
#不存在交易中的订单   返回 -4

def dbSelectDevUpdate(aMsn,aMac,aValue) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_CARD_UPDATE('" + aMsn + "','" + aMac + "'," + aValue + ")</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectstart",tmpRet)
    return tmpRet
    
    
    
#根据 获取单个设备的信息 :设备id
#返回值           成功             设备的信息
#                传入参数为空      返回 -1
#                不存在此设备编号   返回 -2

def dbSelectDevInfo(aMac) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_MY_CARD_GET_DEVINFO('" + aMac + "')</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectstart",tmpRet)
    arr = tmpRet.split("&v;")
    tmp_posid   = arr[0]  #设备四位id
    tmp_dname   = arr[1]  #设备名称
    tmp_mac     = arr[2]  #mac地址，暂时存放设备编号
    tmp_psecond = arr[3]  #每秒单价
    tmp_ptone   = arr[4]  #每吨单价
    tmp_ptype   = arr[5]  #支付类型 0计时1计量2智能
    tmp_stime   = arr[6]  #开始延时（秒）
    tmp_etime   = arr[7]  #结束延时（秒）
    tmp_mharge  = arr[8]  #最低消费（元）
    tmp_lon     = arr[9]  #经度
    tmp_lat     = arr[10] #维度
    #print tmp_posid,tmp_dname,tmp_mac,tmp_psecond,tmp_ptone,tmp_ptype,tmp_stime,tmp_etime,tmp_mharge,tmp_lon,tmp_lat
    return tmpRet
    
#开始加水传入：msn,mac,value,停止方式
#停止方式：0=刷卡停/1=超级用户停/2=定额/3=定时/4=定量/5不足、6=o/7=c
#成功返回       返回0
#传入参数为空   返回-1
#不存在此设备   返回-2
#数据库没有此卡 返回-3
#不存在未完成订单   -4

def dbSelectStop(aMsn,aMac,aValue,aSWay) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_CARD_STOP_WATER('" + aMsn + "','" + aMac + "'," + aValue + "," + aSWay+")</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectstart",tmpRet)
    return tmpRet    
    
#开始加水传入：msn,mac,value
#成功返回       返回0
#传入参数为空   返回-1
#数据库没有此卡 返回-2
#不存在此设备   返回-3
def dbSelectstart(aMsn,aMac,aValue) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_CARD_START_WATER('" + aMsn + "','" + aMac + "'," + aValue + ")</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectstart",tmpRet)
    return tmpRet

#获取 msn 和 mac 地址 
def dbSelectGetMsnMac(aCard,aDid) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_GET_MSN_DID(" + aCard + ",'" + aDid + "')</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectGetMsnMac",tmpRet)
    return tmpRet

#获取单个设备的状态
def dbSelectGetOneDevice(aCard,aDid,aValue) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_GET_ONEDEVICE(" + aCard + ",'" + aDid + "'," + aValue +")</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectGetOneDevice",tmpRet)
    return tmpRet

#根据经纬度，获取的设备列表
def dbSelectGetLonLatList(aLon,aLat) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_GET_LONLATDEV(" + aLon + "," + aLat +")</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectGetLonLatList",tmpRet)
    return tmpRet

#丛数据库中获取使用的列表，未完成的订单
def dbSelectGetUsingList(aCard) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_GET_USINGLIST(" + aCard +")</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectGetUsingList",tmpRet)
    return tmpRet
    
#获取卡的信息
def dbSelectGetCardInfos(aCard) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_GET_CARDINFOS(" + aCard +")</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    print tmpRet
    tmpRet = cmdSplit("dbSelectGetCardInfos",tmpRet)
    return tmpRet

#获取主卡号的手机号    
def dbSelectGetPhone(aCard) :
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_GET_PHONE(" + aCard +")</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd)
    tmpRet = cmdSplit("dbSelectGetPhone",tmpRet)
    return tmpRet
    
    
if __name__ == '__main__':
    print ''
    #print dbSelectGetOrderPrint('10012016121112201561')
    #print dbSelectGetOrderMsg('10022016121016350415')
    #print dbSelectGetOrderList('29121','101')
    #print dbSelectDevUpdate('24B5B806','5730303148FF6B067266545327200667','12')
    #print dbSelectDevInfo('5730303148FF6B067266545327200667')
    #print dbSelectStop('24B5B806','5730303148FF6B067266545327200667','12','0')
    #print dbSelectstart('24B5B806','5730303148FF6B067266545327200667','12')
    #print dbSelectGetMsnMac('29121','1000')
    #print dbSelectGetOneDevice('29121','5730303148FF6F067266545332200667','12')
    #print dbSelectGetLonLatList('40.2072545481','116.6009651198')
    print dbSelectGetUsingList('29121')
    #print dbSelectGetCardInfos('29121')
    #print dbSelectGetPhone('29121')
